package upload;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * 客户端 -- TCP
 */
public class LoadClient {
    Socket socket = null;

    public LoadClient(String ip, int port) throws IOException {
        socket = new Socket(ip,port);
    }
    public void start(){
        Scanner scanner = new Scanner(System.in);
        try(InputStream inputStream = socket.getInputStream()){
            try(OutputStream outputStream = socket.getOutputStream()){
                System.out.print("请输入文件名: ");
                String request = scanner.nextLine();
                PrintWriter printWriter = new PrintWriter(outputStream);
                printWriter.println(request);
                printWriter.flush();

                // 接受数据
                OutputStream outputStream1 = new FileOutputStream("E:/"+request+".png");
                byte[] buffer = new byte[1024];
                int len;
                while ((len = inputStream.read(buffer)) != -1) {
                    outputStream1.write(buffer,0,len);
                }
                outputStream1.flush();
                socket.shutdownOutput();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {

        LoadClient client = new LoadClient("127.0.0.1", 9090);
        client.start();
    }
}
